package yja.gzw.app.activity

import android.animation.ValueAnimator
import android.app.DatePickerDialog
import android.content.Intent
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.bottomsheet.BottomSheetDialog
import yja.gzw.app.R
import yja.gzw.app.base.BaseActivity
import yja.gzw.app.databinding.ActivityWdyuerBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.viewmodel.Mainviewmodel
import com.google.android.material.tabs.TabLayout
import yja.gzw.app.adapter.MinxiAdapter
import java.util.Calendar

class WDYuerActivity : BaseActivity<Mainviewmodel, ActivityWdyuerBinding>(R.layout.activity_wdyuer) {

    private lateinit var fabiada: MinxiAdapter
    var zhuangtai="all"
    val tongjilist = mutableListOf<String>("周统计", "月统计", "年统计")
    val shouzhitab= mutableListOf<String>("全部","收支明细","支出明细")

    private val heights = listOf(150f, 250f, 300f, 150f, 250f, 300f, 250f)
    var money="5"
    var typess=""
    var account=""
    var shijain=""
    private lateinit var bottomSheetDialog: BottomSheetDialog
    override fun onResume() {
        super.onResume()

        binding.xyzhou.setMaxYValue(100f)
        binding.xyzhou.setScaleCountX(10)
        binding.xyzhou.setMaxXValue(10f)
        for (i in heights.indices) {
            animateHeightChange(i, heights[i])
        }
        viewmodel.onyuerwang(UserData.token)

    }
    private fun showDatePickerDialog() {
        val calendar = Calendar.getInstance()
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        val datePickerDialog = DatePickerDialog(
            this,
            { _, selectedYear, selectedMonth, selectedDayOfMonth ->
                val selectedDate = String.format("%d-%02d", selectedYear, selectedMonth + 1)

                binding.jingtishijian.text=selectedDate
                shijain=selectedDate
                viewmodel.onmingxiwang(UserData.token, mapOf("page" to "1","pagesize" to "10","type" to zhuangtai,"time" to shijain))

            },
            year,
            month,
            day
        )
        datePickerDialog.show()
    }

    override fun initView() {
        val calendar = Calendar.getInstance()
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH) + 1 // 月份从0开始，需要加1

        // 格式化日期为 "yyyy-MM-dd" 格式
        val todayDate = String.format("%d-%02d", year, month)

        // 设置今天的日期到 jingtishijian TextView
        binding.jingtishijian.text = todayDate
        shijain=todayDate

        viewmodel.onmingxiwang(UserData.token, mapOf("page" to "1","pagesize" to "10","type" to "all","time" to shijain))



        binding.jingtishijian.setOnClickListener {

            showDatePickerDialog()
        }



        binding.tixianxuanxiang.setOnClickListener {
            val intent=Intent(this,TixianActivity::class.java)
            startActivity(intent)
        }


        binding.tabs.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
              if(tab.position==0){
                   val heights = listOf(150f, 250f, 300f, 150f, 250f, 300f, 250f)
                  sehzhishuju(heights, 50f, 7f, 7)
              }else if(tab.position==1){

                  val heights = listOf(150f, 250f, 300f, 150f, 250f, 300f, 250f, 300f, 250f, 300f)
                  sehzhishuju(heights, 50f, 10f, 10)
              }else{
                  val heights = listOf(150f, 250f, 300f, 150f, 250f, 300f, 250f, 300f,250f, 300f, 250f, 300f)
                  sehzhishuju(heights, 50f, 12f, 12)

              }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })

        binding.xyzhou.setMaxYValue(100f)
        binding.xyzhou.setScaleCountX(10)
        binding.xyzhou.setMaxXValue(10f)
        for (i in heights.indices) {
            animateHeightChange(i, heights[i])
        }
        for (s in shouzhitab.indices) {
            val tab = binding.tabminxi.newTab()
            binding.tabminxi.addTab(tab.setText(shouzhitab[s]))

        }
        binding.tabminxi.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab) {
                if(tab.position==0){
                zhuangtai="all"

                    viewmodel.onmingxiwang(UserData.token, mapOf("page" to "1","pagesize" to "10","type" to zhuangtai,"time" to shijain))

                }else if(tab.position==1){
                    zhuangtai="1"
                    viewmodel.onmingxiwang(UserData.token, mapOf("page" to "1","pagesize" to "10","type" to zhuangtai,"time" to shijain))

                } else{
                    zhuangtai="2"
                    viewmodel.onmingxiwang(UserData.token, mapOf("page" to "1","pagesize" to "10","type" to zhuangtai,"time" to shijain))

                }


            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }


        })



        viewmodel.onyuerwang(UserData.token)
        for (i in tongjilist.indices) {
            val tab = binding.tabs.newTab()
            binding.tabs.addTab(tab.setText(tongjilist[i]))
        }
    }

    fun sehzhishuju(list: List<Float>,y:Float,x:Float,kedu:Int){

        binding.xyzhou.setMaxYValue(y)
        binding.xyzhou.setScaleCountX(kedu)
        binding.xyzhou.setMaxXValue(x)
        for (i in list.indices) {
            animateHeightChange(i, list[i])
        }

    }

    private fun animateHeightChange(index: Int, targetHeight: Float) {
        val animator = ValueAnimator.ofFloat(0f, targetHeight)
        animator.duration = 1000 // 设置动画持续时间为1秒
        animator.addUpdateListener { animation ->
            val animatedValue = animation.animatedValue as Float
            binding.xyzhou.setRectHeight(index, animatedValue)
        }
        animator.start()
    }

    override fun dataObservese() {
        viewmodel.onmingxi.observe(this, Observer {
            if(it.code==1){

                binding.reveiw.layoutManager= LinearLayoutManager(this)
                fabiada= MinxiAdapter(this,it.data.data)
                binding.reveiw.adapter= fabiada




            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
            }

        })


        viewmodel.ontixian.observe(this, Observer {
            if(it.code==1){
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()
                viewmodel.onyuerwang(UserData.token)
                bottomSheetDialog.dismiss()

            }else{
                Toast.makeText(this,it.msg, Toast.LENGTH_SHORT).show()

            }

        })

        viewmodel.onyuer.observe(this, Observer {
            if (it.code == 1) {
                binding.keyongyuer.text = it.data.money.toString()
                binding.yudeiyuer.text = it.data.pre_money.toString()
            } else {
                Toast.makeText(this, it.msg, Toast.LENGTH_SHORT).show()
            }
        })
    }
}
